<?php

namespace app\controllers\merchant\distribution;

use app\models\core\CosModel;
use app\models\core\UploadsModel;
use app\models\merchant\distribution\SuperModel;
use yii;
use yii\web\MerchantController;

/**
 * 超级会员设置
 * @author  wmy
 * Class SuperController
 * @package app\controllers\merchant\distribution
 */
class SuperController extends MerchantController
{

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    public function behaviors()
    {
        return [
            'token' => [
                'class' => 'yii\filters\MerchantFilter', //调用过滤器
                'except' => ['uploads'], //指定控制器不应用到哪些动作
            ]
        ];
    }

    /**
     * 查询单条
     * @return array
     */
    public function actionOne()
    {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数
            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }
            $model = new SuperModel();
            $params['merchant_id'] = yii::$app->session['uid'];
            $array = $model->one($params);
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    /**
     * 上传图片
     * @return array|false|string
     */
    public function actionUploads()
    {
        if (yii::$app->request->isPost) {
            //设置类目 参数
            $upload = new UploadsModel('file', "./uploads/distribution");
            $str = $upload->upload();
            if (!$str) {
                return "上传文件错误";
            }
            $imgModel = new \app\models\core\ImageModel($str, 750);
            $imgModel->compressImg($str);
            // 将图片上传到cos
            $cos = new CosModel();
            $cosRes = $cos->putObject($str);
            if ($cosRes['status'] == '200') {
                $url = $cosRes['data'];
                unlink(Yii::getAlias('@webroot/') . $str);
            } else {
                unlink(Yii::getAlias('@webroot/') . $str);
                return json_encode($cosRes, JSON_UNESCAPED_UNICODE);
            }
            return result(200, "上传成功", $url);
        } else {
            return result(500, "请求方式错误");
        }
    }

    /**
     * 新增
     * @return array
     */
    public function actionAdd()
    {
        if (yii::$app->request->isPost) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参
            //设置类目 参数
            $must = ['name', 'condition', 'cash_back', 'key', 'self_buy', 'recommend_back', 'background_image', 'icon'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }
            $params['merchant_id'] = yii::$app->session['uid'];
            $superModel = new SuperModel();
            $info = $superModel->one(['key' => $params['key'], 'merchant_id' => $params['merchant_id']]);
            if($info['status'] == 200){
                return result(500, "你已设置了");
            }
            $array = $superModel->add($params);
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    /**
     * 编辑
     * @return array
     */
    public function actionUpdate()
    {
        if (yii::$app->request->isPut) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参
            //设置类目 参数
            $must = ['name', 'condition', 'cash_back', 'key', 'self_buy', 'recommend_back', 'background_image', 'icon'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }
            $params['merchant_id'] = yii::$app->session['uid'];
            $superModel = new SuperModel();
            $info = $superModel->one(['key' => $params['key'], 'merchant_id' => $params['merchant_id']]);
            if($info['status'] == 200){
                $array = $superModel->do_update(['id' => $info['data']['id']], $params);
            }else{
                $array = $superModel->add($params);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }
}
